package com.yongbao.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yongbao.common.PageInfo;
import com.yongbao.common.utils.AssertUtil;
import com.yongbao.model.domain.DishSpecificationDO;
import com.yongbao.model.dtomapper.DishDtoMapper;
import com.yongbao.model.domain.DishDO;
import com.yongbao.mapper.DishMapper;
import com.yongbao.model.request.DishRequest;
import com.yongbao.model.util.DishConvert;
import com.yongbao.model.util.DishSpecificationConvert;
import com.yongbao.service.DishService;
import com.yongbao.model.vo.DishVO;
import com.yongbao.service.DishSpecificationService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zhanghong
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, DishDO> implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishSpecificationService dishSpecificationService;

    @Override
    public DishVO getByKey(long id) {
        DishVO dishVO = DishDtoMapper.INSTANCE.do2VO(getById(id));
        return dishVO;
    }

    @Override
    public PageInfo<DishVO> pageQuery(DishRequest request) {
        long count = dishMapper.pageCount(request);
        List<DishDO> list = dishMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<DishVO> dishVOList = DishConvert.buildDishVoList(list);
        for (DishVO dishVO : dishVOList) {
            // 查询规格集合
            List<DishSpecificationDO> specList = dishSpecificationService.lambdaQuery().eq(DishSpecificationDO::getDishId, dishVO.getId()).list();
            dishVO.setSpecifications(DishSpecificationConvert.buildDishSpecificationVoList(specList));
        }
        return new PageInfo(request.getPageNum(), request.getPageSize(), dishVOList, count);
    }

    @Override
    public List<DishVO> listQuery(DishRequest request) {
        List<DishDO> list = dishMapper.selectDOList(DishDtoMapper.INSTANCE.request2DO(request));
        List<DishVO> dishVOList = DishConvert.buildDishVoList(list);
        for (DishVO dishVO : dishVOList) {
            // 查询规格集合
            List<DishSpecificationDO> specList = dishSpecificationService.lambdaQuery().eq(DishSpecificationDO::getDishId, dishVO.getId()).list();
            dishVO.setSpecifications(DishSpecificationConvert.buildDishSpecificationVoList(specList));
        }
        return dishVOList;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean add(DishRequest request) {
        DishDO entity = DishDtoMapper.INSTANCE.request2DO(request);
        if (save(entity) && CollectionUtils.isNotEmpty(request.getSpecifications())) {
            List<DishSpecificationDO> dishSpecificationDOS = DishSpecificationConvert.buildDishSpecificationDOList(entity.getId(), request.getSpecifications());
            dishSpecificationService.saveBatch(dishSpecificationDOS);
        }
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean updateByKey(DishRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        DishDO entity = DishDtoMapper.INSTANCE.request2DO(request);
        if (saveOrUpdate(entity)) {
            // 每次都是删除后重新写入
            dishSpecificationService.lambdaUpdate().eq(DishSpecificationDO::getDishId, entity.getId()).remove();
            List<DishSpecificationDO> dishSpecificationDOS = DishSpecificationConvert.buildDishSpecificationDOList(entity.getId(), request.getSpecifications());
            dishSpecificationService.saveBatch(dishSpecificationDOS);
        }
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean delByIds(String ids) {
        String[] dishIds = Convert.toStrArray(ids);
        dishMapper.deleteDOByIds(dishIds);
        dishSpecificationService.lambdaUpdate().in(DishSpecificationDO::getDishId, dishIds).remove();
        return true;
    }
}
